<?php
 /**     
  *
  * Copyright (c) 2009, Persistent Systems Limited
  *
  * Redistribution and use, with or without modification, are permitted 
  *  provided that the following  conditions are met:
  *   - Redistributions of source code must retain the above copyright notice, 
  *     this list of conditions and the following disclaimer.
  *   - Neither the name of Persistent Systems Limited nor the names of its contributors 
  *     may be used to endorse or promote products derived from this software 
  *     without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
/**
 * Class to send cURL request
 * @access public
 */
class HttpCurlRequestLocal
{	
    /** 
     * Class constructor
	 * @access public
	 */
	public function __construct()
	{
		if (!extension_loaded('curl')) {
            throw ('cURL extension has to be enable to use cURL services.');
        }
	}
	/**
	 * This function prepare CURL Request according to parameters passed and send HTTP request
	 * @access public
	 *
	 * @param string $httpVerb Request Method
	 * @param string $url Restful URI path
	 * @param array $variables Array of data to be send with request
	 * @param array $headers Array of data that need to be send with request header
	 * @param bool $isSetProxy TRUE if Proxy set else False by default
	 * @return array $response Array of Response Info
	 */
	public function sendRequest($httpVerb = 'GET', $url, $variables = array(), $headers = array(), $isSetProxy = false)
	{
		$response = array();
		try{
			$handle = curl_init();
			$curlHeaders = array();
			foreach ($headers as $key => $value)
			{
			  $curlHeaders[] = $key.': '.$value;
			}
			if(count($curlHeaders))
			{
				curl_setopt($handle, CURLOPT_HTTPHEADER, $curlHeaders); 
			}

			curl_setopt($handle, CURLOPT_RETURNTRANSFER, true) ;
			curl_setopt($handle,CURLOPT_URL, $url);
			curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);

			switch ($httpVerb) {
				case 'GET':
					curl_setopt($handle, CURLOPT_HTTPGET, true);
					break;
				case 'POST':
					curl_setopt($handle, CURLOPT_POST,    true);
					break;
				case 'PUT':
					
					$data = http_build_query($variables);
					$tenMB = 10 * 1024 * 1024;   
					$fh = fopen("php://temp/maxmemory:{$tenMB}",'rw');
					fwrite($fh, $data);
					rewind($fh);
					
					curl_setopt($handle, CURLOPT_INFILE, $fh);   
					curl_setopt($handle, CURLOPT_INFILESIZE, strlen($data));   
					curl_setopt($handle, CURLOPT_PUT, true);						

					break;
				case 'DELETE':
					curl_setopt($handle, CURLOPT_CUSTOMREQUEST,  'DELETE');
					curl_setopt($handle, CURLOPT_NOBODY, true);
					break;
				default:
					curl_setopt($handle, CURLOPT_CUSTOMREQUEST,  $httpVerb);
					break;
			}
			if ($isSetProxy){
				curl_setopt($handle, CURLOPT_PROXY, $proxyUrl);
				curl_setopt($handle, CURLOPT_PROXYPORT, $proxyPort);
			}
			curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, $isSetProxy);	

			if (!is_null($variables) && count($variables) > 0){
				curl_setopt($handle, CURLOPT_POSTFIELDS,  $variables);
			}
			
			$response['requestInfo']  = curl_getinfo($handle);
			$response['response'] = curl_exec($handle);	
			$responseHeader = curl_getinfo($handle);
			$response['http_code'] = $responseHeader['http_code'];
			$response['curl_errors']  = curl_error ($handle);
			
			curl_close($handle);

		} catch(Exception $e){
			throw($e);
		}
		return $response;
	}
}
?>